#include "general.h"

#include <buola/algorithm/container.h>

void CGConf_Error_Handler(GConfClient *pClient,GError *pError)
{
}

CGConfBranch::CGConfBranch()
{
	g_thread_init(nullptr);
	g_type_init();

	mClient=gconf_client_get_default();
	gconf_client_set_global_default_error_handler(CGConf_Error_Handler);
}
#if 0
bool CGConfBranch::GetValue(const std::wstring &pFolder,const std::wstring &pKey,
							UValue &pValue)
{
	GConfEntry *lEntry=gconf_client_get_entry(mClient,
							utf8(pFolder+L'/'+pKey).c_str(),nullptr,true,nullptr);
	GConfValue *lValue=gconf_entry_get_value(lEntry);

	if(lValue)
	{
		switch(lValue->type)
		{
		case GCONF_VALUE_STRING:
			pValue.Set(utf32(gconf_value_get_string(lValue)));
			break;
		case GCONF_VALUE_INT:
			pValue.Set<int>(gconf_value_get_int(lValue));
			break;
		case GCONF_VALUE_FLOAT:
			pValue.Set<double>(gconf_value_get_float(lValue));
			break;
		case GCONF_VALUE_BOOL:
			pValue.Set<bool>(gconf_value_get_bool(lValue));
			break;
		default:
			msg_warn() << "wrong value type in CGConfBranch::GetValue\n";
			return false;
		}

		return true;
	}
	return false;
}
#endif
bool CGConfBranch::GetLeafList(const std::string &pFolder,std::vector<std::string> &pList)
{
	GSList *lKey=gconf_client_all_entries(mClient,("/"+pFolder).c_str(),nullptr);

	while(lKey)
	{
		std::string lString=gconf_entry_get_key((GConfEntry*)lKey->data);
		pList.push_back(lString.substr(lString.rfind('/')+1));
		lKey=lKey->next;
	}

	return true;
}
#if 0
bool CGConfBranch::GetKeyValueList(const std::wstring &pFolder,boost::ptr_vector<CKeyValue> &pList)
{
	GSList *lKey=gconf_client_all_entries(mClient,utf8(pFolder).c_str(),nullptr);

	while(lKey)
	{
		std::string lString=gconf_entry_get_key((GConfEntry*)lKey->data);
		CKeyValue &lNew=ptr_push_back_new(pList);
		lNew.mKey=utf32(lString.substr(lString.rfind('/')+1));

		GConfValue *lValue=gconf_entry_get_value((GConfEntry*)lKey->data);

		switch(lValue->type)
		{
		case GCONF_VALUE_STRING:
			lNew.mValue.Set(utf32(gconf_value_get_string(lValue)));
			break;
		case GCONF_VALUE_INT:
			lNew.mValue.Set<int>(gconf_value_get_int(lValue));
			break;
		case GCONF_VALUE_FLOAT:
			lNew.mValue.Set<double>(gconf_value_get_float(lValue));
			break;
		case GCONF_VALUE_BOOL:
			lNew.mValue.Set<bool>(gconf_value_get_bool(lValue));
			break;
		default:
			msg_warn() << "wrong value type in CGConfBranch::GetValue\n";
			break;
		}

		lKey=lKey->next;
	}

	return true;
}

bool CGConfBranch::SetValue(const std::wstring &pFolder,const std::wstring &pKey,
							const UValue &pValue)
{
	std::string lKey=utf8(pFolder+L'/'+pKey);

	if(pValue.HasType<int>())
		gconf_client_set_int(mClient,lKey.c_str(),pValue.Get<int>(),nullptr);
	else if(pValue.HasType<unsigned int>())
		gconf_client_set_int(mClient,lKey.c_str(),pValue.Get<unsigned int>(),nullptr);
	else if(pValue.HasType<std::wstring>())
		gconf_client_set_string(mClient,lKey.c_str(),utf8(pValue.Get<std::wstring>()).c_str(),nullptr);
	else if(pValue.HasType<std::string>())
		gconf_client_set_string(mClient,lKey.c_str(),pValue.Get<std::string>().c_str(),nullptr);
	else if(pValue.HasType<double>())
		gconf_client_set_float(mClient,lKey.c_str(),pValue.Get<double>(),nullptr);
	else if(pValue.HasType<bool>())
		gconf_client_set_bool(mClient,lKey.c_str(),pValue.Get<bool>(),nullptr);
	else
	{
		msg_warn() << "wrong value in CGConfBranch::SetValue\n";
		return false;
	}

	return true;
}
#endif
